//###############
//FILE : WantServersRequest.java
//WRITER : Daniel Lowengrub, daniel_i_l, 320687098
//EXERCISE : oop ex3 2011
//DESCRIPTION:
//See below
//###############

package oop.ex3.nameserver;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import oop.ex3.protocol.sessions.NameWantServersSession;

/**
 * This handles the WANTSERVERS request as specified in the assignment.
 * @author daniel
 */
public class WantServersRequest extends FileManagerSpecificRequest{
    private NameWantServersSession _wantServersSession;

    /**
     * Create a new request initialized with a NS database which is passed to the super class.
     * The opening session is a NameWantServersSession.
     * @param nsDb
     */
    
    public WantServersRequest(NameServerDatabase fileDb) {
        super(fileDb, new NameWantServersSession());
        _wantServersSession = (NameWantServersSession)_openingSession;
    }

    @Override
    public boolean serveRequest(DataInputStream inputStream, DataOutputStream outputStream) {
        //System.out.println("serving want servers request");

        //first run the want all files session
        _wantServersSession.setNameServers(_nameServerDb.getNameServers());

        if (!_wantServersSession.executeSession(inputStream, outputStream)) {
            return false;
        }


        //if we didn't find the end  then there was an error
        if (!_endSession.executeSession(inputStream, outputStream)) {
            return false;
        }
        
        return true;
    }

    @Override
    public NameServerRequest getCopy() {
        WantServersRequest copy = new WantServersRequest(_nameServerDb);
        return copy;
    }

}
